package com.tj.bo.common.conf.util;


import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author: binary
 * 2018/8/9
 */
@Component
public class HttpClient {

    private static final Logger LOGGER = LoggerFactory.getLogger(HttpClient.class);


    public static final okhttp3.MediaType MEDIA_TYPE = okhttp3.MediaType.parse("application/json; charset=utf-8");

    @Autowired(required = false)
    private RestTemplate restTemplate;


    /**
     * 发送get请求
     */
    public String getData(String url, Object... uriVariables) {
        return getData(url, String.class, uriVariables);
    }

    /**
     * 发送get请求
     */
    public <T> T getData(String url, Class<T> responseType, Object... uriVariables) {
        return restTemplate.getForObject(url, responseType, uriVariables);
    }

    /**
     * 发送post请求
     * @param url  请求URL地址
     * @param data json数据
     */
    public String postJsonData(String url, HttpHeaders headers, String data) {
        HttpEntity<String> formEntity = new HttpEntity<>(data, headers);
        return restTemplate.postForObject(url, formEntity, String.class);
    }

    /**
     * 发送post请求
     * @param url  请求URL地址
     * @param data json数据
     */
    public <T> T postJsonData(String url, HttpHeaders headers, String data, Class<T> tClass) {
        HttpEntity<String> formEntity = new HttpEntity<>(data, headers);
        return restTemplate.postForObject(url, formEntity, tClass);
    }

    /**
     * 发送post请求
     * @param url  请求URL地址
     * @param data json数据
     */
    public String postJsonData(String url, String data) {
        return postJsonData(url, data, String.class);
    }

    /**
     * 发送post请求
     * @param url  请求URL地址
     * @param data json数据
     */
    public <T> T postJsonData(String url, String data, Class<T> responseType) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Accept", "application/json");
        headers.add("Content-Encoding", "UTF-8");
        headers.add("Content-Type", "application/json; charset=UTF-8");
        HttpEntity<String> formEntity = new HttpEntity<>(data, headers);
        return restTemplate.postForObject(url, formEntity, responseType);
    }

    /**
     * post 提交表单数据
     * application/x-www-form-urlencoded
     */
    public String postFormData(String url, Map<String, String> params, Object... uriVariables) {
        return postFormData(url, params, String.class, uriVariables);
    }

    /**
     * post 提交表单数据
     * application/x-www-form-urlencoded
     */
    public <T> T postFormData(String url, Map<String, String> params, Class<T> responseType, Object... uriVariables) {
        return postFormData(url, params, responseType, MediaType.APPLICATION_FORM_URLENCODED, uriVariables);
    }

    /**
     * post 提交表单数据
     * multipart/form-data
     */
    public <T> T postMultipartFormData(String url, Map<String, String> params, Class<T> responseType, Object... uriVariables) {
        return postFormData(url, params, responseType, MediaType.MULTIPART_FORM_DATA, uriVariables);
    }

    public <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) {
        return restTemplate.exchange(url, method, requestEntity, responseType, uriVariables);
    }

    /**
     * post 提交表单数据
     */
    private <T> T postFormData(String url, Map<String, String> params, Class<T> responseType, MediaType mediaType, Object... uriVariables) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(mediaType);
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.setAll(params);
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        return restTemplate.postForObject(url, request, responseType, uriVariables);
    }

    public String post(String url, String json) {
        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(25, TimeUnit.SECONDS).build();

        RequestBody body = RequestBody.create(MEDIA_TYPE, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        try {
            Response response = client.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            LOGGER.error("======== post error ,e " + e);
        }
        return null;
    }

}
